weather_df = 
  rnoaa::meteo_pull_monitors(
    c("USW00094728", "USW00022534", "USS0023B17S"),
    var = c("PRCP", "TMIN", "TMAX"), 
    date_min = "2021-01-01",
    date_max = "2022-12-31") |>
  mutate(
    name = case_match(
      id, 
      "USW00094728" ~ "CentralPark_NY", 
      "USW00022534" ~ "Molokai_HI",
      "USS0023B17S" ~ "Waterhole_WA"),
    tmin = tmin / 10,
    tmax = tmax / 10) |>
  select(name, id, everything())
## using cached file: /Users/zhangshizhe/Library/Caches/org.R-project.R/R/rnoaa/noaa_ghcnd/USW00094728.dly
## date created (size, mb): 2024-09-26 10:23:14.171516 (8.651)
## file min/max dates: 1869-01-01 / 2024-09-30
## using cached file: /Users/zhangshizhe/Library/Caches/org.R-project.R/R/rnoaa/noaa_ghcnd/USW00022534.dly
## date created (size, mb): 2024-09-26 10:23:25.989412 (3.932)
## file min/max dates: 1949-10-01 / 2024-09-30
## using cached file: /Users/zhangshizhe/Library/Caches/org.R-project.R/R/rnoaa/noaa_ghcnd/USS0023B17S.dly
## date created (size, mb): 2024-09-26 10:23:29.76067 (1.036)
## file min/max dates: 1999-09-01 / 2024-09-30
weather_df |> 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5)
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

Make a scatterlot but fancy

weather_df |> 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5) + 
  labs(
    title = "Temperature plot",
    x = "Minimum daily temperature (C)",
    y = "Maxiumum daily temperature (C)",
    color = "Location",
    caption = "Data from the rnoaa package"
  )
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

Scales –

weather_df |> 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5) + 
  labs(
    title = "Temperature plot",
    x = "Minimum daily temperature (C)",
    y = "Maxiumum daily temperature (C)",
    color = "Location",
    caption = "Data from the rnoaa package"
  ) + 
  scale_x_continuous(
    breaks = c(-15, 0, 20),
    labels = c("-15C", "0", "20")
  ) +
  scale_y_continuous(
    limits = c(0, 30),
    transform = "sqrt"
  )
## Warning in transformation$transform(x): NaNs produced
## Warning in scale_y_continuous(limits = c(0, 30), transform = "sqrt"): sqrt
## transformation introduced infinite values.
## Warning: Removed 302 rows containing missing values or values outside the scale range
## (`geom_point()`).

Look at color

ggp_scatterplot = weather_df |> 
  ggplot(aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5) + 
  labs(
    title = "Temperature plot",
    x = "Minimum daily temperature (C)",
    y = "Maxiumum daily temperature (C)",
    color = "Location",
    caption = "Data from the rnoaa package") + 
  scale_color_hue(h = c(100, 300)) +
  viridis::scale_color_viridis(discrete = TRUE)
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.

Themes

ggp_scatterplot +
  theme(legend.position = "bottom")
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggp_scatterplot +
  theme_bw() +
  theme(legend.position = "bottom")
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggp_scatterplot +
  theme(legend.position = "bottom") +
  theme_minimal()
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggp_scatterplot +
  theme(legend.position = "bottom")+
  theme_classic()
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggp_scatterplot +
  theme(legend.position = "bottom") +
  ggthemes::theme_excel()
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

LA

ggplot(weather_df, aes(x = date, y = tmax, color = name)) + 
  geom_smooth(se = FALSE) + 
  geom_point(aes(size = prcp), alpha = .75) + 
  labs(
    title = "Temperature plot",
    x = "Date",
    y = "Maxiumum daily temperature (C)",
    color = "Location",
    caption = "Data from the rnoaa package"
  ) + 
  viridis::scale_color_viridis(discrete = TRUE) + 
  theme_minimal() + 
  theme(legend.position = "bottom")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 19 rows containing missing values or values outside the scale range
## (`geom_point()`).

Extra bonus in ggplot Use different datasets in different ’geom’s

central_park_df =
  weather_df |> 
  filter(name == "CentralPark_NY")

molokai_df = 
  weather_df |> 
  filter(name == "Molokai_HI")

molokai_df |> 
  ggplot(aes(x=date, y=tmax, color=name))+
  geom_point()+
  geom_line(data = central_park_df)
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).

Mutiple panels

weather_df |> 
  ggplot(aes(x = tmax, fill = name))+
  geom_density()+
  facet_grid( ~name)
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_density()`).

patch plots

ggp_tmax_tmin =
  weather_df |> 
  ggplot(aes(x = tmin, y = tmax, color = name))+
  geom_point(alpha = .3)

ggp_tmax_density =
  weather_df |> 
  ggplot(aes(x = tmax, fill = name))+
  geom_density(alpha = .3)

ggp_tmax_date =
  weather_df |> 
  ggplot(aes(x = date, y = tmax, color = name))+
  geom_point(alpha = .3) +
  geom_smooth(se=FALSE)

(ggp_tmax_tmin + ggp_tmax_density)/ggp_tmax_date
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_density()`).
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

Data manipulation

weather_df |>
  mutate(name = forcats::fct_relevel(name, c("Molokai_HI", "CentralPark_NY", "Waterhole_WA"))) |> 
  ggplot(aes(x = name, y = tmax)) + 
  geom_violin(aes(fill = name), color = "blue", alpha = .5) + 
  theme(legend.position = "bottom")
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_ydensity()`).